(function (mui, $) {
var page  = window.page = {};
//评论类型   1:好评,2:中评,3:差评
var _Data = {'token':app.store('token'), 'page':0, 'nums':20, 'id':'', 'type':''};

page.init = function (data) {
	_Data.id = data.id;
	call_on_pull_down();
}
page.refresh = function () {
	call_on_pull_down();
}

function next (cb) {
	app.log('请求参数：', mui.extend({}, _Data, {'page':_Data.page+1, 'data':{'goods_id':_Data.id, 'type':_Data.type}}));
	if (plus.webview.currentWebview().isVisible()) { plus.nativeUI.showWaiting(); }
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Goods/CommentNotSign/lists'),
		'data'     : mui.extend({}, _Data, {'page':_Data.page+1, 'data':{'goods_id':_Data.id, 'type':_Data.type}}),
	})
	.fail(function (res) {
		// 失败回调
		plus.nativeUI.closeWaiting();
		app.log('获取评论列表失败：', res);
		app.toast('获取评论列表失败');
		app.tryResetPullLoading();
	})
	.done(function (res) {
		// 成功回调
		plus.nativeUI.closeWaiting();
		app.log('评论数据：', res);
		
		if (500 == res.code) { app.toast('你还未登录'); app.tryResetPullLoading(); return; }
		if (300 == res.code) { app.toast(res.msg); return; }
		if (200 != res.code) { app.toast('请求失败'); return; }
		if (res.msg && plus.webview.currentWebview().isVisible()) { app.toast(res.msg); }
		
		if (res.data && res.data.length) { _Data.page++; }
		cb && cb(res);
		
		if (res.count) {
			$('.total span>b').text(res.count.total);
			$('.perfect span>b').text(res.count.type_1);
			$('.notbad span>b').text(res.count.type_2);
			$('.awful span>b').text(res.count.type_3);
		}
	})
	;
}
// 下拉刷新
function call_on_pull_down () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	_Data.page = 0;
	$(pullrefresh.element).find('.mui-scroll>.scroll-data').empty();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		$(dom).append(app.tpl($(dom).attr('data-tpl'), res));
		pullrefresh.endPulldownToRefresh();
		
		mui.previewImage();
	});
}
// 上拉加载
function call_on_pull_up () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		var ele = app.tpl($(dom).attr('data-tpl'), res);
		if ($(ele).hasClass('no-data')) {
			if (0 == _Data.page) { $(dom).empty(); } else { $(ele).remove(); ele = null; }
		}
		$(dom).append(ele);
		pullrefresh.endPullupToRefresh();
		
		mui.previewImage();
	});
}
mui.init({
	pullRefresh   : {
		container : '.container-pull-refresh',
		down      : {callback : call_on_pull_down},
		up        : {callback : call_on_pull_up},
	}
});

mui.plusReady(function() {
	var view = plus.webview.currentWebview();
	setTimeout(function () {
		page.init(view.extras);
	}, 1000);
});

// 切换评论分类
$('[name=type]').on('change', function () {
	_Data.type = $(this).val();
	call_on_pull_down();
});

// 点赞评论
$('body').delegate('.btn-awesome', 'tap', function () {
	var dom = this, view = plus.webview.currentWebview();
	
	app.log('请求参数：', {'token':app.store('token'), 'data':{'comment_id':$(dom).closest('.judge').attr('data-id')}});
	plus.nativeUI.showWaiting();
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Goods/CommentNotSign/like'),
		'data'     : {'token':app.store('token'), 'data':{'comment_id':$(dom).closest('.judge').attr('data-id')}},
	})
	.fail(function (res) {
		// 失败回调
		plus.nativeUI.closeWaiting();
		app.log('点赞失败：', res);
		app.toast('点赞失败');
	})
	.done(function (res) {
		// 成功回调
		plus.nativeUI.closeWaiting();
		app.log('点赞结果：', res);
		
		if (500 == res.code) { app.toast('你还未登录'); return; }
		if (300 == res.code) { app.toast(res.msg); return; }
		if (200 != res.code) { app.toast('请求失败'); return; }
		if (res.msg) { app.toast(res.msg); }
		
		$(dom).text(' ' + res.data.likes);
	})
	;
})

})(mui, $)
